<?php

require_once ('Zend/Rest/Server.php');
require_once ('Zend/Soap/Client.php');

class YumApiController extends YumController {

	public function accessRules()
	{
		return array(
			array('allow', 
				'actions'=>array('authLogin', 'authLogout', 'authProfile', 'authSearch', 'login', 'inviter', 'DboLogin'),
				'users'=>array('*'),
			),
			array('deny',  
				'users'=>array('*'),
			),
		);
	}
	
	public function actionAuthLogin() {
		if (isset($_REQUEST ['redirect_url'])) {			
			$data = array ('redirect_url' => $_REQUEST ['redirect_url'] );
			$request = array ('method' => 'loginSSO', 'data' => $data );
			$server = new Zend_Rest_Server ();
			$server->setClass ( 'YumAuthServer' );
			$respone = $server->handle ( $request );
		}
	}
	
	public function actionAuthLogout() {
	
	}
	
	public function actionAuthProfile() {
		if(!empty($_REQUEST ['auth_token'])){		
			$data = array ('token' => $_REQUEST ['auth_token']);
			$request = array ('method' => 'getProfile', 'data' => $data );
			$server = new Zend_Rest_Server ();
			$server->setClass ( 'YumAuthServer' );
			$server->handle ( $request );
		}	
		return false;
	}
	
	public function actionAuthSearch() {
		$data = array ('word' => $_REQUEST ['word'], 'searchflag' => $_REQUEST ['searchflag'] );
		$request = array ('method' => 'searchUser', 'data' => $data );
		$server = new Zend_Rest_Server ();
		$server->setClass ( 'YumAuthServer' );
		$server->handle ( $request );
	}
	
	public function actionLogin() {
		if(isset($_POST['username']) && isset($_POST['password'])) {
			$data = array('username' => $_POST['username'], 'password' => $_POST['password']);
			$request = array ('method' => 'login', 'data' => $data );
			$server = new Zend_Rest_Server ();
			$server->setClass ( 'YumAuthServer' );
			$server->handle ( $request );			
		}
	}
	
	public function actionInviter() {
		if(isset($_REQUEST ['e'])) {
			$data = array ('email' => $_REQUEST ['e']);
			$request = array ('method' => 'getInviter', 'data' => $data );
			$server = new Zend_Rest_Server ();
			$server->setClass ( 'YumAuthServer' );
			$server->handle ( $request );
		}
	}
	
	public function actionDboLogin() {
		if(isset($_POST['userAccount']) && isset($_POST['password']) && isset($_POST['sign'])) {
			$model = new LoginForm();
			$model->username = urldecode($_POST['userAccount']);
			if(isset($_POST['password'])){
				$password = base64_decode($_POST['password']);
				$password = explode(' ', $password);
				$password = $password[1];
				$model->password = $password;
			} else {
				$model->password = null;
			}
			
			if(isset($_POST['user_ip'])){
				$sign = strtoupper(md5($model->username.$_POST['password'].$_POST['user_ip'].Yii::app()->params->dbo_login_key));
			} else {
				$sign = strtoupper(md5($model->username.$_POST['password'].Yii::app()->params->dbo_login_key));
			}
			$model->validate();
			if (!$model->errors && $sign == $_POST['sign']) {
				$login = $model->checkLogin();
				if($login) {
					Yii::app()->user->setLastvisit();
					$xml = new DOMDocument('1.0', 'utf-8');
					$response = $xml->createElement('response', '');
					$result = $xml->createElement('result', 1);
					$adult = $xml->createElement('adult', 1);
					$userName = $xml->createElement('userName', $login->id);
					$response->appendChild($result);
					$response->appendChild($adult);
					$response->appendChild($userName);
					$xml->appendChild($response);
					echo $xml->saveXML();
				} 
			} elseif ($sign != $_POST['sign']) {
				$xml = new DOMDocument('1.0', 'utf-8');
				$response = $xml->createElement('response', '');
				$result = $xml->createElement('result', -1813);
				$response->appendChild($result);
				$xml->appendChild($response);
				echo $xml->saveXML();
			} else {
				if(count($model->errors) > 0){
					$xml = new DOMDocument('1.0', 'utf-8');
					$response = $xml->createElement('response', '');
					$result = $xml->createElement('result', -1811);
					$response->appendChild($result);
					$message = $xml->createElement('message', 'USERNAME_OR_PASSWORD_INVALID');
					$response->appendChild($message);
					$xml->appendChild($response);
				}
				echo $xml->saveXML();
				
			}
		} else {
				$xml = new DOMDocument('1.0', 'utf-8');
				$response = $xml->createElement('response', '');
				$result = $xml->createElement('result', -1811);
				$message = $xml->createElement('message', 'MISSING_PARAMETER');
				$response->appendChild($result);
				$response->appendChild($message);
				$xml->appendChild($response);
				echo $xml->saveXML();
		}
	}
}